/*
 * gsmmux.c -- file of functions for gsmmux
 *
 * Copyright (C) 2007 lufang <lufang@gmail.com>
 *
 * Version 1.0 june 2007
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>
#include "gsm_module.h"
#include "gsm0710.h"
#include "mtk.h"
#include "m1190.h"
#include "utils.h"

static const GeneratorCreateFunc s_generator_funcs[] = 
{
	mtk_get_generator,
	m1190_get_generator,
	//simware_get_generator,
	NULL
};

static GsmModule* gsm_module_create_with_config (const char* module_type, const char* device_name)
{
	GsmModule*	module = NULL;
	GsmModuleGenerator* generator = NULL;
	int i = 0;

	while (s_generator_funcs[i] != NULL)
	{
		generator = s_generator_funcs[i]();
		if (generator->match_func(generator, module_type))
		{
			module = generator->create_module_func(generator, device_name);
		}
		generator->destroy_func(generator);
		if (module != NULL)
		{
			break;
		}
		
		i++;
	}

	return module;
}

GsmModule* gsm_module_create (const char* module_type, const char* device_name)
{
	GsmModule* 			module = NULL;

	if (module_type != NULL && device_name != NULL)
	{
		module = gsm_module_create_with_config(module_type, device_name);
	}

	return module;
}

void gsm_module_destroy (GsmModule* module)
{
	if (module != NULL)
	{
		module->module_destroy(module);
	}
	
	return;
}

static void usage(const char* exe)
{
	GSMMUX_PRINT("usage: %s -t <modem type> -d <device path>\n", exe);

	return;
}

int main(int argc, char** argv)
{
	GsmModule* thiz = NULL;
	int opt = 0;
	const char* type = "mtk";
	const char* device = "/dev/ttyS0";
	
	while ( -1 != (opt = getopt(argc, argv, "t:d:"))) {
        switch (opt) {
            case 't':
				type = optarg;
	            break;

            case 'd':
                device = optarg;
				break;

            default:
                usage(argv[0]);
                return 0;
        }
    }
	thiz = gsm_module_create(type, device);
	
	while(1)
	{
		sleep(10000);
	}

	return 1;
}


